package com.ezgo.erc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ezgo.erc.entity.Syslog;
import com.ezgo.erc.master.utils.Constants;
import com.ezgo.erc.master.utils.Page;
import com.ezgo.erc.master.utils.StringUtil;
import com.ezgo.erc.service.SyslogService;
import com.google.common.collect.Maps;

/**
 * 
 * (系统)模块 TODO(系统日志信息视图控制层)
 * 
 * @author jyx
 * @date 2015年12月15日 下午5:37:13
 *
 */
@Controller
@RequestMapping(value = "/sys/syslog")
public class SyslogController extends BaseController {

	private Map<String, Object> map = Maps.newHashMap();
	private String tipmess = "";

	@Autowired
	private SyslogService syslogService;

	@ModelAttribute
	public Syslog get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return syslogService.getSyslog(id);
		} else {
			return new Syslog();
		}
	}

	@RequestMapping(value = { "list", "" })
	public String list(Syslog syslog, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response, Model model) {
		try {
			map.clear();
			if (syslog != null) {
				if (syslog.getId() != null) {
					map.put("id", syslog.getId() + "");
				}
				if (syslog.getType() != null) {
					map.put("type", syslog.getType() + "");
				}
			}

			Page<Syslog> page = syslogService.find(new Page<Syslog>(request, response), map);
			model.addAttribute("page", page);
		} catch (Exception e) {
			tipmess = syslog.getExMsg();
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[3][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
		}
		return "sys/syslogList";
	}

	@RequestMapping(value = "view")
	public String view(Syslog syslog, RedirectAttributes redirectAttributes, Model model) {
		try {
			model.addAttribute("syslog", syslog);
		} catch (Exception e) {
			tipmess = syslog.getExMsg();
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[3][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
			return "redirect:/sys/syslog/?repage";
		}
		return "sys/syslogView";
	}

}
